package com.wangsb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @Description: TODO
 * @author: shenbing.wang@hand-china
 * @date: 2021年02月24日 22:28
 */
@Controller
public class ExceptionController {

    @RequestMapping("/exception01")
    public String exception(@RequestParam("username") String username) {
        System.out.println("方法处理中01");
        return "show";
    }

    @RequestMapping("/exception02")
    public String exception02(@RequestParam("username") String username) {
        System.out.println("方法处理中01");
        return "show";
    }

    @ExceptionHandler({Exception.class})
    public ModelAndView handleException(Exception exception) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error");
        modelAndView.addObject("ex", exception);
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        exception.printStackTrace(pw);
        System.out.println(sw.toString());
        return modelAndView;
    }

}
